import json
from bson import ObjectId
class Role:
    name=""
    
class User:
    name=""
    roles=""
    id="aaa"

u=User()
u.name="test"
r=Role()
r.name="aaaa"
u.id=ObjectId("637f06e7c2c48ad59d80c73d")


u.roles={"role":[r for i in range(10)]}


print(json.loads(json.dumps(u,default=lambda o:o.__dict__ if not isinstance(o,ObjectId) else str(o))))